#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_equip.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_unit","_equipment_type","_equipment_var","_backpack_array","_backpack","_x","_weapon","_equipment_counter","_unit_equipment","_i"];
DEFAULT_PARAM(_unit,0,objNull);
DEFAULT_PARAM(_equipment_type,1,"");
DEFAULT_PARAM(_equipment_counter,2,0);

if (!isNull _unit && (!isPlayer _unit || local _unit)) then {
	if ((_unit getVariable ["unit_equiped",""]) != _equipment_type) then {			
		if (_equipment_counter > 1) then {
			{
				if (_x == leader (group _unit)) then {
					_unit_equipment = Format["%1%2",toUpper(_equipment_type),1];
					[_x, _unit_equipment] call EFUNC(equip);
				} else {
					_unit_equipment = Format["%1%2",toUpper(_equipment_type),(floor(random(_equipment_counter - 1)) + 2)];
					[_x, _unit_equipment] call EFUNC(equip);
				};
			} foreach units (group _unit);
		} else {
			_equipment_var = format ["%1_EQUIPMENT",toUpper(_equipment_type)];
			if (!isNil _equipment_var && !(_unit getVariable ["edt_fnc_equip_ignore",false])) then {
				_equipment = missionNameSpace getVariable _equipment_var;
				_unit setVariable ["unit_equiped",_equipment_type];
				if (!local _unit) then {
					TRACE_1("Non local AI Unit",ARR_2(_unit,GVAR(JIP)));
					if (!GVAR(JIP)) then {
						_uniform = _equipment select 4;
						TRACE_1("Equipping Uniform",ARR_2(_unit,_uniform));
						if (count _uniform > 0) then {
							TRACE_1("Current Uniform",ARR_5(_unit, uniform _unit, vest _unit, headgear _unit, goggles _unit));
							if ((uniform _unit) != (_uniform select 0)) then {
								removeUniform _unit;
								sleep 0.3;
								while { (uniform _unit) != (_uniform select 0) } do {
									_unit forceAddUniform (_uniform select 0);
									 sleep 0.3;
								};
							} else {
								_unit forceAddUniform (_uniform select 0);
								sleep 0.3;
								TRACE_1("Already has Uniform",ARR_3(_unit, uniform _unit, (_uniform select 0)));
							};
							TRACE_1("New Uniform",ARR_5(_unit, uniform _unit, vest _unit, headgear _unit, goggles _unit));
						};
					} else {
						TRACE_1("JIP Not doing anything",ARR_5(_unit, uniform _unit, vest _unit, headgear _unit, goggles _unit));
					};
				} else {
					removeallweapons _unit;
					removeBackpack _unit;
					removeallItems _unit;
					removeAllAssignedItems _unit;
					{ _unit removeMagazines _x;
					} foreach (magazines _unit);
					if (typename _equipment == typename []) then {
						TRACE_1("Equipment Found",count _equipment);
						if (count _equipment > 4) then {
							_uniform = _equipment select 4;
							TRACE_1("Equipping Uniform",_uniform);
							TRACE_1("Current Uniform",ARR_5(_unit, uniform _unit, vest _unit, headgear _unit, goggles _unit));
							if (count _uniform > 0) then {
								removeHeadgear _unit;
								removeGoggles _unit;
								removeVest _unit;
								sleep 0.2;
								if ((uniform _unit) != (_uniform select 0)) then {
									removeUniform _unit;
									sleep 0.2;
									while { (uniform _unit) != (_uniform select 0) } do {
										_unit forceAddUniform (_uniform select 0);
										 sleep 0.2;
									 };
								} else {
									_unit forceAddUniform (_uniform select 0);
									 sleep 0.1;
								};
							};
							if (count _uniform > 1) then {
								if ((_uniform select 1) != "") then { _unit addVest (_uniform select 1); sleep 0.1; };
							};
							if (count _uniform > 2) then {
								if ((_uniform select 2) != "") then { _unit addHeadgear (_uniform select 2); sleep 0.1; };
							};
							if (count _uniform > 3) then {
								if ((_uniform select 3) != "") then { _unit addGoggles (_uniform select 3); sleep 0.1; };
							};
							TRACE_1("New Uniform",ARR_5(_unit, uniform _unit, vest _unit, headgear _unit, goggles _unit));
						};
						if (count _equipment > 1) then {
							TRACE_1("Adding Magazines",ARR_2(_unit, (_equipment select 1)));
							{
								if (isClass(configFile/"CfgMagazines"/_x)) then {
									_unit AddMagazine _x;
								} else {
									_unit addItem _x;
								};
							} foreach (_equipment select 1);
						};
						if (count _equipment > 0) then {
							TRACE_1("Equipping Weapons",ARR_2(_unit, (_equipment select 0)));
							 sleep 0.1;
							{ 
								if (typename _x == typename []) then {
									_items = _x;
									if (count _items > 0) then { _unit AddWeapon (_items select 0); };
									if (count _items > 1) then {
										for "_i" from 1 to ((count _items) - 1) step 1 do {
											if (primaryWeapon _unit == (_items select 0)) then {
												_unit addPrimaryWeaponItem (_items select _i);
											};
											if (secondaryWeapon  _unit == (_items select 0)) then {
												_unit addSecondaryWeaponItem (_items select _i);
											};
											if (handgunWeapon  _unit == (_items select 0)) then {
												_unit addHandgunItem (_items select _i);
											};
										};
									};
								} else {
									_unit AddWeapon _x;
								};
							} foreach (_equipment select 0);
						};
						if (count _equipment > 3) then {
							TRACE_1("Assigning Items",ARR_2(_unit, (_equipment select 3)));
							 sleep 0.1;
							{	if (typename _x == typename []) then {
									_unit addItem (_x select 0);
									_unit assignItem (_x select 0);
									sleep 0.01;
								} else {
									_unit addItem _x;
								};
							} foreach (_equipment select 3);
						};
						if (count _equipment > 2) then {
							_backpack_array = (_equipment select 2);
							if (typename _backpack_array == typename []) then {
								_backpack = objNull;
								if (count _backpack_array > 0 && {(_backpack_array select 0) != ""}) then {
									TRACE_1("Adding Backpack",ARR_2(_unit, (_backpack_array select 0)));
									_unit addBackpack (_backpack_array select 0);
									_backpack = unitBackpack _unit;
									 sleep 0.1;
								};
								if (!isnull _backpack) then {
									clearMagazineCargo _backpack;
									clearWeaponCargo _backpack;
									clearAllItemsFromBackpack _unit;
									if (count _backpack_array > 1) then {
										TRACE_1("Adding Backpack Weapons",ARR_2(_unit, (count (_backpack_array select 1))));
										{ _backpack addWeaponCargo [_x, 1];
										} foreach (_backpack_array select 1);
									};
									if (count _backpack_array > 2) then {
										TRACE_1("Adding Backpack Mags",ARR_2(_unit, (count (_backpack_array select 2))));
										{ _backpack addMagazineCargo [_x, 1];
										} foreach (_backpack_array select 2);
									};
									if (count _backpack_array > 3) then {
										TRACE_1("Adding Backpack Items",ARR_2(_unit, (count (_backpack_array select 3))));
										{ _unit addItemToBackpack _x;
										} foreach (_backpack_array select 2);
									};
								};
							};
						};
					} else {
						ERROR_1("Equipment not found",ARR_2(_unit, _equipment_var));
					};
					if (count (weapons _unit) > 0) then {
						TRACE_1("Selecting Weapon",ARR_2(_unit, primaryWeapon _unit));
						_weapon = primaryWeapon _unit;
						if (!isnil "_weapon") then {
							_unit selectWeapon _weapon;
						} else {
							_weapon = secondaryWeapon _unit;
							if (!isnil "_weapon") then {
								_unit selectWeapon _weapon;
							};
						};
					};
				};
			};
		};
	} else {
		TRACE_1("Unit already equipped",ARR_2(_unit, _unit getVariable "unit_equiped"));
	};
} else {
	TRACE_1("Unit Not Local",ARR_2(_unit, local _unit));
};

TRACE("End");
TMFPROFILERSTOP;